package com.github.ginirohikocha.dbm.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.github.ginirohikocha.dbm.R;
import com.github.ginirohikocha.dbm.fragment.MeFragment;
import com.github.ginirohikocha.dbm.fragment.OrderFragment;
import com.github.ginirohikocha.dbm.util.XingeUtil;

public class MainActivity extends AppCompatActivity {

    private OrderFragment orderFragment;

    private LinearLayout barOrder, barMe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        orderFragment = OrderFragment.newInstance();
        getSupportFragmentManager().beginTransaction()
                .add(R.id.main_container, orderFragment)
                .commit();

        SharedPreferences workerInfo = getSharedPreferences("worker", 0);
        Toast.makeText(MainActivity.this, "欢迎回来，"+workerInfo.getString("workerName", ""), Toast.LENGTH_SHORT).show();

        String account = workerInfo.getString("mobilephone", "");
        if (account.isEmpty()) {
            startActivity(new Intent(MainActivity.this, LoginActivity.class));
            finish();
        } else {
            XingeUtil.initXG(getApplicationContext(), account);
        }

        //底部菜单初始化
        barOrder  = findViewById(R.id.bar_order);//业务
        barMe = findViewById(R.id.bar_me);//我的
        barOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.main_container, orderFragment)
                        .commit();
            }
        });
        barMe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.main_container, MeFragment.newInstance())
                        .commit();
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    orderFragment.refresh();
                    Toast.makeText(MainActivity.this, data.getStringExtra("msg"), Toast.LENGTH_LONG).show();
                }
        }
    }

    public void refresh() {
        orderFragment.refresh();
    }

    private long clickTime=0;
    /**
     *  点击两次退出
     * **/
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (SystemClock.uptimeMillis() - clickTime <= 1500) {
                //如果两次的时间差＜1s，就不执行操作
            } else {
                //当前系统时间的毫秒值
                clickTime = SystemClock.uptimeMillis();
                Toast.makeText(MainActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                return false;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
